//
//  ProgressBar.swift
//  Kosspot
//
//  Created by 胡辉 on 2025/7/3.
//

import SwiftUI

struct ProgressBar: View {
    let segments: Int
    @Binding var currentPage: Int
    var activeColor: Color = Color(hex: "#00F3AF")
    var inactiveColor: Color = Color(hex: "#404040")
    var animationDuration: Double = 0.3
    
    private var segmentWidth: CGFloat {
        1.0 / CGFloat(segments)
    }
    
    var body: some View {
        GeometryReader { geometry in
            ZStack(alignment: .leading) {
                
                RoundedRectangle(cornerRadius: 5)
                    .fill(inactiveColor)
                    .frame(height: 10)
                
                RoundedRectangle(cornerRadius: 5)
                    .fill(activeColor)
                    .frame(width: geometry.size.width * segmentWidth, height: 10)
                    .offset(x: calculateHighlightPosition(width: geometry.size.width))
                    .animation(.easeInOut(duration: animationDuration), value: currentPage)
                
            }
        }
        .frame(height: 10)
    }
    
    private func calculateHighlightPosition(width: CGFloat) -> CGFloat {
        let segmentWidth = width / CGFloat(segments)
        return CGFloat(currentPage) * segmentWidth
    }
    
    private func calculateIndicatorPosition(width: CGFloat) -> CGFloat {
        let segmentWidth = width / CGFloat(segments)
        return CGFloat(currentPage) * segmentWidth + segmentWidth / 2
    }
}

#Preview {
    ProgressBar(segments: 5, currentPage: .constant(2))
}

